package com.wangzhenyu.common.base

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.RomUtils
import com.blankj.utilcode.util.ToastUtils
import com.wangzhenyu.common.R
import com.wangzhenyu.common.base.delegate.DefaultLoadingDelegate
import com.wangzhenyu.common.base.delegate.LoadingDelegate
import com.wangzhenyu.common.view.EmptyLayout


abstract class BaseFragment<P : BasePresenter> : Fragment(), BaseView {

    protected lateinit var mContext: Context
    protected var mPresenter: P? = null

    //对Dialog的操作
    protected var mLoadingDelegate: LoadingDelegate? = null

    //没有数据时显示的EmptyLayout
    protected var elEmpty: EmptyLayout? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initBundles()
        mPresenter = createPresenter()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        parent: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (getLayoutId() == null || getLayoutId() == 0) {
            return null
        }
        //不使用基础布局
        if (!useBaseLayout()) {
            return inflater.inflate(getLayoutId()!!, parent, false)
        }

        // 将子布局添加到父布局中
        val rootView = inflater.inflate(R.layout.fragment_base, parent, false)
        val containerView = rootView.findViewById<FrameLayout>(R.id.containerView)
        inflater.inflate(getLayoutId()!!, containerView, true)
        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (useBaseLayout()) {
            elEmpty = view.findViewById(R.id.elEmpty)
        }

        initViewsAndData(view)
    }

    private fun isPageFinish() = (mContext as Activity).isFinishing || elEmpty == null

    override fun showPageLoading() {
        if (isPageFinish()) return
        elEmpty?.setErrorType(EmptyLayout.LOADING)
    }

    override fun hidePageLoading() {
        if (isPageFinish()) return
        elEmpty?.setErrorType(EmptyLayout.HIDE_LAYOUT)
    }

    override fun showNoData() {
        if (isPageFinish()) return
        elEmpty?.setErrorType(EmptyLayout.NO_DATA)
    }

    override fun showNoNetwork() {
        if (isPageFinish()) return
        elEmpty?.setErrorType(EmptyLayout.NO_NETWORK)
    }

    override fun showDataError() {
        if (isPageFinish()) return
        elEmpty?.setErrorType(EmptyLayout.DATA_ERROR)
    }

    override fun showReason(text: String) {
        if (isPageFinish()) return
        elEmpty?.setErrorText(text)
    }

    override fun showDialogLoading() {
        getLoadingDelegate().showDialogLoading(getString(R.string.loading))
    }

    override fun showDialogLoading(content: String?) {
        getLoadingDelegate().showDialogLoading(content)
    }

    override fun hideDialogLoading() {
        getLoadingDelegate().hideDialogLoading()
    }

    override fun showToast(content: String?) {
        if (!TextUtils.isEmpty(content)) {
            val toast = ToastUtils.make()
            if (RomUtils.isXiaomi()) {
                toast.setBgColor(ContextCompat.getColor(mContext, R.color.color_CC000000))
            } else {
                toast.setBgResource(R.drawable.shape_toast_bg)
            }
            toast.setGravity(Gravity.CENTER, 0, 100)
            toast.setTextColor(ContextCompat.getColor(mContext, R.color.common_ffffff))
            toast.show(content)
        }
    }

    override fun showBottomToast(content: String?) {
        if (!TextUtils.isEmpty(content)) {
            val toast = ToastUtils.make()
            if (RomUtils.isXiaomi()) {
                toast.setBgColor(ContextCompat.getColor(mContext, R.color.color_CC000000))
            } else {
                toast.setBgResource(R.drawable.shape_toast_bg)
            }
            toast.setGravity(Gravity.BOTTOM, 0, 100)
            toast.setTextColor(ContextCompat.getColor(mContext, R.color.common_ffffff))
            toast.show(content)
        }
    }

    protected fun getLoadingDelegate(): LoadingDelegate {
        if (mLoadingDelegate == null) {
            mLoadingDelegate = DefaultLoadingDelegate(mContext)
        }
        return mLoadingDelegate as LoadingDelegate
    }

    protected abstract fun getLayoutId(): Int?

    protected open fun initBundles() {
        // do nothing
    }

    protected abstract fun createPresenter(): P?

    protected abstract fun initViewsAndData(rootView: View)

    open fun useBaseLayout() = true


}